package org.tagging.component.security;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import org.tagging.component.jwt.JwtTokenGenerator;
import org.tagging.component.jwt.JwtTokenPair;
import org.tagging.model.TaggingUser;
import org.tagging.model.TaggingUserAuthority;
import org.tagging.service.impl.other.AuthorityServiceImpl;
import org.tagging.service.impl.other.AuthorityUserServiceImpl;
import org.tagging.service.impl.other.ClientUserServiceImpl;
import org.tagging.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Component
public class LoginSuccessHandler implements AuthenticationSuccessHandler {

    @Autowired
    private JwtTokenGenerator jwtTokenGenerator;

    @Autowired
    private AuthorityServiceImpl authorityService;

    @Autowired
    private AuthorityUserServiceImpl authorityUserService;

    @Autowired
    private ClientUserServiceImpl userDetailsService;


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        userDetailsService.updateLastLoginTime(userDetails.getUsername());
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        Set<String> roles = new HashSet<>();
        for (GrantedAuthority authority : authorities) {
            roles.add(authority.getAuthority());
        }
        Map<String, String> additional = new HashMap<>();
        JwtTokenPair jwtTokenPair = jwtTokenGenerator.jwtTokenPairWithUsername(userDetails.getUsername(), roles, additional);
        Map<String, Object> data = new HashMap<>();
        List<String> allAusRole = authorityService.queryAllAuthorities();
        Map<String, Boolean> map = new HashMap<>(allAusRole.size());
        List<TaggingUserAuthority> taggingUserAuthorities = authorityUserService.queryTemplate();
        for (TaggingUserAuthority taggingUserAuthority : taggingUserAuthorities) {
            allAusRole.add(taggingUserAuthority.getAuthority());
        }
        for (String allAu : allAusRole) {
            map.put(allAu, roles.contains(allAu));
        }
        //此处获取用户所拥有的权限
//        TaggingUser currentUser = userDetailsService.queryByUsername(userDetails.getUsername(), TaggingUser.Column.id);
//        Integer userId = currentUser.getId();
//        List<TaggingUserAuthority> taggingUserAuthorities = authorityUserService.queryByUserId(userId);//当前用户所拥有的个人权限
//        for (TaggingUserAuthority taggingUserAuthority : taggingUserAuthorities) {
//            map.put(taggingUserAuthority.getAuthority(), true);
//        }
//        List<String> aus = new ArrayList<>(authorities.size());
//        for (GrantedAuthority authority : authorities) {
//            aus.add(authority.getAuthority());
//        }
        data.put("authority", map);
        data.put("token", jwtTokenPair);
        String build = ResponseUtils.build(HttpStatus.OK.value(), "Login success!", data);
        ResponseUtils.printJson(response, build);
    }


}
